package com.dzt.androidframework.express;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.dzt.androidframework.R;
import com.dzt.androidframework.activity.ActivityTitle;
import com.dzt.androidframework.express.javabean.OrderDetailEvent;
import com.dzt.androidframework.express.javabean.OrderInfoDetailBean;
import com.dzt.androidframework.express.javabean.OrderState;
import com.dzt.androidframework.utils.GsonErrorLinkTreeMapHelper;
import com.dzt.androidframework.utils.JTimeKit;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

public class OrderDetailActivity extends ActivityTitle {

	@ViewInject(R.id.tv_order_detail_head_status)
	TextView tvActivityOrderdetailHeadStatus;
	@ViewInject(R.id.tv_order_detail_head_num)
	TextView tvActivityOrderdetailHeadNum;
	@ViewInject(R.id.tv_order_time)
	TextView tvActivityOrderdetailTime;
	@ViewInject(R.id.rv_order_detail)
	RecyclerView rv_activity_orderdetail;

	private String state;
	private OrderDetailAdapter madapter;
	private List datas = new ArrayList<>();

	@Override
	protected int getLayoutId() {
		return R.layout.activity_order_detail;
	}

	@Override
	protected void initParams() {
		setTitleView(R.string.order_detail);
	}

	@Override
	protected void initData(Bundle savedInstanceState) {

	}

	/**
	 * 接收消息函数在主线程，且为粘性事件
	 * From FramgmentOne
	 */
	@Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
	public void onEvent(OrderDetailEvent response) {
		datas = GsonErrorLinkTreeMapHelper.duleTree2List(response.Data.getData(), OrderInfoDetailBean.class);
		setHeadView(response);
		setRecycleView(response);
	}

	/**
	 * 设置头部
	 */
	private void setHeadView(OrderDetailEvent response) {
		if (null != datas && datas.size() > 0) {
			tvActivityOrderdetailHeadStatus.setText(OrderState.caseState(response.Data.getState()));
			tvActivityOrderdetailHeadNum.setText(response.Data.getNu());
			int i = datas.size() - 1;//开始时间为最后一条信息
			OrderInfoDetailBean bean = (OrderInfoDetailBean) datas.get(i);
			String startTime = bean.getTime();
			int j = 0;//结束时间为第0条信息
			OrderInfoDetailBean bean2 = (OrderInfoDetailBean) datas.get(j);
			String endTime = bean2.getTime();
			tvActivityOrderdetailTime.setText("耗时：" + JTimeKit.getTimeDifferenceDay(startTime, endTime));
		}
	}

	/**
	 * 设置列表
	 */
	private void setRecycleView(OrderDetailEvent response) {
		if (null != datas && datas.size() > 0) {
			state = response.Data.getState();
			rv_activity_orderdetail.setLayoutManager(new LinearLayoutManager(this));
			madapter = new OrderDetailAdapter(OrderDetailActivity.this, datas, R.layout.item_order_detail, state);
			rv_activity_orderdetail.setAdapter(madapter);
		}
	}

	@Override
	protected String[] initPermissions() {
		return new String[0];
	}
}
